package com.yanzhang.service.bill;

import com.yanzhang.dao.bill.BillDao;
import com.yanzhang.dao.bill.BillDaoImpl;
import com.yanzhang.pojo.Bill;
import com.yanzhang.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yanzhang
 */
public class BillServiceImpl implements BillService{

    private BillDao billDao;

    public BillServiceImpl() {
        billDao = new BillDaoImpl();
    }

    /**
     * 增加一个账单
     * @param bill 账单对象
     * @return 是否成功
     */
    @Override
    public boolean addBill(Bill bill) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int result = billDao.add(connection, bill);
            connection.commit();
            if (result > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("新增账单失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }

    /**
     * 条件查询
     * @param bill 账单对象
     * @return 账单对象集合
     */
    @Override
    public List<Bill> getBillList(Bill bill) {
        List<Bill> bills = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        try {
            bills = billDao.getBillList(connection, bill);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return bills;
    }

    /**
     * 根据id获取账单对象
     * @param id 主键
     * @return 账单对象
     */
    @Override
    public Bill getBillById(int id) {
        Bill bill = null;
        Connection connection = DBUtil.getConnection();
        try {
            bill = billDao.getBillById(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return bill;
    }

    /**
     * 修改账单信息
     * @param bill 账单对象
     * @return 是否成功
     */
    @Override
    public boolean modifyBill(Bill bill) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int result = billDao.modify(connection, bill);
            connection.commit();
            if (result > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("修改账单失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }

    /**
     * 通过id删除一个账单
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteBillById(int id) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int result = billDao.deleteBillById(connection, id);
            connection.commit();
            if (result > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("删除账单失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }
}
